package com.auth.manager.service.sys.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth.manager.common.ex.BizException;
import com.auth.manager.mapper.sys.UserInfoMapper;
import com.auth.manager.pojo.sys.UserInfo;
import com.auth.manager.service.sys.UserInfoService;
import com.auth.manager.utils.JwtTokenUtil;
import com.auth.manager.utils.PasswordUtil;
import com.auth.manager.utils.page.PageUtil;
import com.auth.manager.utils.page.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @version 1.0.0
 * @Date: 2022/4/28 20:50
 * @Author ZhuYouBin
 * @Description 系统用户
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void createAccount(UserInfo userInfo) {
        // 判断账号是否存在
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("account_no", StrUtil.trim(userInfo.getAccountNo()));
        wrapper.last("limit 1");
        long count = this.count(wrapper);
        if (count > 0) {
            throw new BizException("账号已存在");
        }
        // 密码加密
        userInfo.setPassword(PasswordUtil.encode(userInfo.getPassword()));
        String nickname = userInfo.getNickname();
        if (StrUtil.isBlank(nickname)) {
            userInfo.setNickname("匿名用户");
        }
        userInfo.setRegisterTime(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss"));
        // 保存数据
        this.save(userInfo);
    }

    @Override
    public void removeAccount(String uid) {
        // 查询是否存在账号
        UserInfo userInfo = this.getById(uid);
        if (Objects.isNull(userInfo)) {
            throw new BizException("登录账号不存在");
        }
        userInfo.setDeleted("02");
        // 更新账号状态
        this.saveOrUpdate(userInfo);
    }

    @Override
    public void updateAccount(UserInfo userInfo) {
        // 查询登录账号
        UserInfo currentUserInfo = this.getById(userInfo.getUid());
        if (Objects.isNull(currentUserInfo)) {
            throw new BizException("登录账号不存在");
        }
        // 判断账号是否存在
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("account_no", StrUtil.trim(userInfo.getAccountNo()));
        wrapper.ne("uid", userInfo.getUid());
        wrapper.last("limit 1");
        long count = this.count(wrapper);
        if (count > 0) {
            throw new BizException("账号已存在");
        }
        // 密码加密
        userInfo.setPassword(PasswordUtil.encode(userInfo.getPassword()));
        // 更新数据
        this.saveOrUpdate(userInfo);
    }

    @Override
    public PageVo queryPage(UserInfo userInfo, PageVo pageVo) {
        PageUtil.start(pageVo); // 开启分页
        userInfo.setDeleted("01");
        List<UserInfo> list = userInfoMapper.queryByCondition(userInfo);
        return PageUtil.end(list);
    }

    @Override
    public Map<String, String> getUserInfo(String token) {
        Map<String, String> map = new HashMap<>();
        map.put("avatar", "");
        map.put("username", "");
        // 解析token
        if (!JwtTokenUtil.isTokenValid(token)) {
            String username = JwtTokenUtil.getUsernameFromToken(token);
            // TODO 从数据库或者 Redis 里面获取用户信息
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.select("account_no", "avatar");
            wrapper.eq("account_no", StrUtil.trim(username));
            wrapper.last("limit 1");
            UserInfo userInfo = this.getOne(wrapper);
            if (!Objects.isNull(userInfo)) {
                map.put("avatar", userInfo.getAvatar());
                map.put("username", userInfo.getAccountNo());
            }
        }
        return map;
    }

    @Override
    public void logout(String token) {
        // TODO 删除redis中当前用户的token数据

    }
}
